<?php

namespace controller;

class LoginController{
	
	/**
	 * LoginModel
	 */
	private $LoginModel;
	
	/**
	 * PageView
	 */
	private $PageView;
	
	/**
	 * Boolean gets passed to the view class to decide what html to render
	 */
	private $isLoggedIn = FALSE;
	
	public function __construct(\model\LoginModel $LoginModel, \view\Pageview $PageView){
		$this -> LoginModel = $LoginModel;
		$this -> PageView = $PageView;
	}
	
	public function doApplication(){
		if($this -> LoginModel -> didUserLogOut()){	
			$this -> PageView -> removeCookies();
		} else {
			$this -> checkLogin();
		}
		$this -> PageView -> setMessage($this -> LoginModel -> getMessageId());
		$this -> doView($isLoggedIn);
	}
	
	/**
	 * @return Boolean returns true if the user succeded in logging in
	 */
	private function checkLogin(){
		$userName = $this -> PageView -> getPostUsername();
		$password = $this -> PageView -> getPostPassword();
		$validUsername = $this -> LoginModel -> user -> userName;
		$validHashedPassword =  $this -> LoginModel -> getHashedPassword();
		$rememberMe = $this -> PageView -> keepLoggedInCheckbox();
				
		if($this -> LoginModel -> isLoggedInBySession()){
			$this -> isLoggedIn = TRUE;
		} elseif($this -> checkCookies()){
			$this -> isLoggedIn = TRUE;
		} elseif($this -> LoginModel -> checkUserInfo($userName, $password, $rememberMe)){
			$this -> isLoggedIn = TRUE;
			if($this -> PageView -> keepLoggedInCheckbox()){
				$this -> PageView -> setCookie($validUsername, $validHashedPassword);
				$this -> LoginModel -> setCookieTimer();
			}
		} else{
			if($this -> PageView -> wasPost()){
				$this -> LoginModel -> typeOfLogin($userName, $password);
				$loginResult = $this -> LoginModel -> getMessageId();
				if($loginResult == 5 || $loginResult == 6 || $loginResult == 7){
					$this -> PageView -> lastUsername = $userName;
				}
			}		
			$this -> isLoggedIn = FALSE;
		}
	}
	//TODO ändra så att den första check cookies bara kollar om det finns kakor, den andra validerar allt
	private function checkCookies(){
		$validUsername = $this -> LoginModel -> user -> userName;
		$validHashedPassword =  $this -> LoginModel -> getHashedPassword();
		$areCookiesPresent = $this -> PageView -> areSomeCookiesSet();
		$areCookiesValid = $this -> PageView -> validateAllCookies($validUsername, $validHashedPassword, $this -> LoginModel -> getCookieTimer());
		// $areCookiesPresent = $this -> PageView -> checkCookies($validUsername, $validHashedPassword);
		// $areCookiesValid = $this -> PageView -> validateCookies($validUsername, $validHashedPassword);
		
		if($this -> LoginModel -> isLoggedInByCookies($areCookiesPresent, $areCookiesValid)){
			$this -> LoginModel -> setSession();
			return TRUE;
		} else {
			$this -> PageView -> removeCookies();
			return FALSE;
		}
	}
	
	/**
	 * @param boolean whether the user is logged in or not
	 */
	private function doView($isLoggedIn){		
		if($this -> isLoggedIn){
			$this -> PageView -> loggedInStatus = $this -> PageView -> loggedIn;
			
			$this -> PageView -> renderHTML(TRUE);
		} else {
			$this -> PageView -> loggedInStatus = $this -> PageView -> notLoggedIn;
			
			$this -> PageView -> renderHTML(FALSE);
		}		
	}
}